package app.hanxiao.com.demo.push.Factory;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.google.gson.Gson;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.text.SimpleDateFormat;
import java.util.List;

import app.hanxiao.com.demo.R;
import app.hanxiao.com.demo.push.bean.PushBean;


public class MyClientHandler extends IoHandlerAdapter {

	private Context context;
	private static final String TAG = "PushServer";
	public MyClientHandler(Context context){
		this.context = context;
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		Log.d(TAG,"执行exceptionCaught ====>         消息发送异常！！！");
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		String msg = (String) message;
		Log.d(TAG,"服务器返回消息 ====>     " + msg);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		//GSON直接解析成对象
		//PushBean resultBean = new Gson().fromJson(msg,PushBean.class);
		//对象中拿到集合
		//List<PushBean.MsgBean> msgBeanList = resultBean.getAddresses();
//		for (int i = 0; i < msgBeanList.size(); i++){
//			Log.d(TAG,"Address ====>     " + msgBeanList.get(i).getAddress());
//			Log.d(TAG,"City ====>     " + msgBeanList.get(i).getCity());
//			Log.d(TAG,"Pc ====>     " + msgBeanList.get(i).getPc());

			NotificationManager messageNotificatioManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
			Notification messageNotification = new NotificationCompat.Builder(context)
					.setContentTitle("服务器消息：")
//					.setContentText("Address："+msgBeanList.get(i).getAddress()+"。City："+msgBeanList.get(i).getCity()
//							+"。Pc"+msgBeanList.get(i).getPc())
					.setContentText("Address："+msg)
					.setWhen(System.currentTimeMillis())
					.setSmallIcon(R.mipmap.ic_launcher)
					.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
					.setAutoCancel(true)
					.build();
			messageNotificatioManager.notify(0,messageNotification);
//		}





	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		Log.d(TAG,"执行messageSent ====>        消息发送成功.......");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		Log.d(TAG,"执行sessionClosed ====>      连接被关闭.........");

	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		Log.d(TAG,"执行messageSent ====>         连接已经建立.........");
	}
	//客户端进入空闲状态打�?
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		Log.d(TAG,"执行messageSent ====>          客户端进入空闲状态.......");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		Log.d(TAG,"执行messageSent ====>          连接已经打开.......");
	}

}
